package com.hu.zhcc.shiro.controller.xsx;


import com.hu.zhcc.shiro.entity.xsx.*;
import com.hu.zhcc.shiro.service.xsx.TGozuoTojiService;
import com.hu.zhcc.shiro.service.xsx.TInterposeSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 干预方案表 前端控制器
 * </p>
 *
 * @author xsx
 * @since 2021-06-21
 */
@RestController
@RequestMapping("/tInterposeScheme")
public class TInterposeSchemeController {

    @Autowired
    TInterposeSchemeService tInterposeSchemeService;
    @Autowired
    TGozuoTojiService tGozuoTojiService;

    //添加
    @PostMapping("")
    public int addScheme(@RequestBody XsxTInterposeScheme scheme){
        scheme.setSchStatue("禁用");
        XsxTGozuoToji tGozuoToji = tGozuoTojiService.queryByJks(scheme.getHealthMgrId());
        if(tGozuoToji==null){
            XsxTGozuoToji t=new XsxTGozuoToji();
            t.setJks(scheme.getHealthMgrId());
            t.setGyfanNum(1);
            t.setGyhfNum(0);
            t.setGyjsNum(0);
            tGozuoTojiService.addOne(t);
        }
        if(tGozuoToji!=null){
            XsxTGozuoToji t=new XsxTGozuoToji();
            t.setJks(scheme.getHealthMgrId());
            t.setGyfanNum(tGozuoToji.getGyfanNum()+1);
            t.setGyhfNum(tGozuoToji.getGyhfNum());
            t.setGyjsNum(tGozuoToji.getGyjsNum());
            tGozuoTojiService.updateNum(t);
        }
        return tInterposeSchemeService.addScheme(scheme);
    }

    //修改
    @PutMapping("/{id}")
    public int updateStatue(@PathVariable("id") Integer id){
        return tInterposeSchemeService.updateStatue(id);
    }

    //根据会员ID查询
    @PostMapping("/{hyid}")
    public List<XsxTInterposeScheme> querySchemeByHyid(@PathVariable("hyid") Integer hyid){
        return tInterposeSchemeService.querySchemeByHyid(hyid);
    }
}

